// pages/medical/schedule/index.js
import {
  hospitalApi,
  getPatientByIdnum
} from "../../../utils/hospital-api";
const dayjs = require('../../../utils/dayjs.min.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    loading1: false,
    dates: [],
    activeDate: '',
    dateFull: '',
    weeks: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    hosCode: '',
    deptId: '',
    docId: '',
    schedule: {}, //当天的排版信息
    scheduleData: [],
    doctorInfo: {}, //医生详情
    currScheduleData: [], //当前日期的排班数据
    schState: {
      0: '立即预约',
      1: '已过期',
      2: '停诊',
      3: '已满',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 保存页面参数信息
    this.setData({
      hospital: wx.getStorageSync('hospital'),
      dept: wx.getStorageSync('dept'),
      doctor: wx.getStorageSync('doctor'),
      hosCode: options.hosCode,
      deptId: options.deptId,
      docId: options.docId,
      activeDate: options.activeDate,
      dateFull: options.dateFull,
    })
    this.initDate()
    this.getDoctor();
    this.getSchedule();
  },
  // 初始化时间数据
  initDate() {
    let dates = [];
    for (let i = 0; i < 8; i++) {
      let date = new Date();
      date.setDate(date.getDate() + i);
      dates.push({
        dateFull: dayjs(date).format('YYYY-MM-DD'),
        date: dayjs(date).format('MM-DD'),
        day: date.getDay(),
        schedules: []
      });
    }
    console.log(this.data.activeDate)
    if (this.data.activeDate) {
      this.setData({
        dates: dates,
      })
    } else {
      this.setData({
        dates: dates,
        activeDate: dates[0].date,
        dateFull: dates[0].dateFull,
      })
    }
  },
  // 时间改变的事件
  changeDate(e) {
    const schedule = e.mark.item;
    this.setData({
      activeDate: schedule.date,
      dateFull: schedule.dateFull,
      schedule: schedule
    })
    this.handleScheduleData();
  },
  // 获取当前日期的排版数据 分上午和下午 这里的数据每次都要动态获取 因为实时有人在预约
  handleScheduleData() {
    this.setData({
      currScheduleData: [],
      loading1: true
    })
    hospitalApi({
      method: 'getSchedule',
      data: JSON.stringify({
        hosCode: this.data.hosCode,
        depId: this.data.deptId,
        docId: this.data.docId,
        startDate: this.data.dateFull,
        endDate: this.data.dateFull,
        isWithSection: '1'
      })
    }).then(res => {
      this.setData({
        currScheduleData: res.data,
        loading1: false
      })

    })
  },
  // 获取医生详情
  getDoctor() {
    this.setData({
      loading: true
    })
    hospitalApi({
      method: 'getDoctor',
      data: JSON.stringify({
        hosCode: this.data.hosCode,
        depId: this.data.deptId,
        docId: this.data.docId,
        isWithIntro: '1',
        isWithSchedule: '1',
        isWithSection: '1'
      })
    }).then(res => {
      this.setData({
        doctorInfo: res.data[0],
        loading: false
      })
    })
  },
  // 根据医生查询后8天的排版信息
  getSchedule() {
    this.setData({
      loading1: true
    })
    hospitalApi({
      method: 'getSchedule',
      data: JSON.stringify({
        hosCode: this.data.hosCode,
        depId: this.data.deptId,
        docId: this.data.docId,
        startDate: this.data.dates[0].dateFull,
        endDate: this.data.dates[7].dateFull
      })
    }).then(res => {
      res.data.forEach(item => {
        this.data.dates.forEach(date => {
          if (item.WORKDATE == date.dateFull) {
            date.schedules.push(item)
          }
        })
      })
      this.setData({
        scheduleData: this.data.dates,
        loading1: false
      })

      this.setData({
        // activeDate: this.data.scheduleData[0].date,
        // dateFull: this.data.scheduleData[0].dateFull,
        schedule: this.data.scheduleData[0]
      })
      this.handleScheduleData();
    })
  },
  // 跳转预约
  reservation(e) {
    // 需要时间段预约的医院就保存时间段的信息
    let url;
    const parent = e.mark.parent;
    wx.setStorageSync('schedule', parent); //保存选中的排班信息
    url = `/pages/medical/reservation/index?schCode=${this.data.schedule.SCHCODE}`
    if (this.data.hospital.ISSEG == 1) {
      const item = e.mark.item;
      wx.setStorageSync('scheduleTime', item); //保存选中的时间信息
      url = `/pages/medical/reservation/index?schCode=${this.data.schedule.SCHCODE}&secCode=${item.SECCODE}`
    }
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo != '') {
      wx.navigateTo({
        url: url
      })
    } else {
      hospitalApi({
        method: 'getPatientByIdnum',
        data: JSON.stringify({
          idnum: userInfo.idNumber
        })
      }).then(res => {
        if (res.code == 1000) {
          // wx.setStorageSync('user', res.data)
          wx.navigateTo({
            url: url
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})